package cn.xorange.commons.utils.poi.excel.handler;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.xorange.commons.utils.spring.SpringUtils;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @program: la-2.0
 * @description: Excel字典表数据导出转换处理
 * @author: gonghe
 * @create: 2021-03-10 10:15
 */
@Configuration
public class ExcelDictHandler implements IExcelDictHandler {

    @Resource
    ExcelDictService excelDictService;

    @Override
    public String toName(String dict, Object obj, String name, Object value) {
//        RemoteDictService remoteDictService = SpringUtils.getBean(RemoteDictService.class);
//        ResultBean ajaxResult = remoteDictService.dictTypeData(dict);
//        if(ajaxResult.getCode() == ResultCode.SUCCESS.getCode()) {
//            JSONArray result = (JSONArray) ajaxResult.getData();
//            List<SysDictData> dictDataList = result.toJavaList(SysDictData.class);
//            Map<String, String> collect = dictDataList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//            return collect.get(String.valueOf(value));
//        }
        //ExcelDictService excelDictService = SpringUtils.getBean(ExcelDictService.class);
        Object nameValue = excelDictService.toNameMap(dict).get(String.valueOf(value));
        return nameValue == null ? "": nameValue.toString();
    }

    @Override
    public String toValue(String dict, Object obj, String name, Object value) {
//        RemoteDictService remoteDictService = SpringUtils.getBean(RemoteDictService.class);
//        ResultBean ajaxResult = remoteDictService.dictTypeData(dict);
//        if(ajaxResult.getCode() == ResultCode.SUCCESS.getCode()) {
//            JSONArray result = (JSONArray) ajaxResult.getData();
//            List<SysDictData> dictDataList = result.toJavaList(SysDictData.class);
//            Map<String, String> collect = dictDataList.stream().collect(Collectors.toMap(SysDictData::getDictLabel,SysDictData::getDictValue));
//            return collect.get(name);
//        }
//        return null;
        //ExcelDictService excelDictService = SpringUtils.getBean(ExcelDictService.class);
        Object valValue = excelDictService.toValueMap(dict).get(String.valueOf(name));

        return valValue == null ? "": valValue.toString();
    }


}
